goto done;
                }
 
-               if ( id == base->e_id ) {
-                       e = base;
-               } else {
-
-                       /* get the entry */
-                       rs->sr_err = mdb_id2edata( op, mci, id, &edata );
-                       if ( rs->sr_err == MDB_NOTFOUND ) {
-                               if( !MDB_IDL_IS_RANGE(candidates) ) {
-                                       /* only complain for non-range IDLs */
-                                       Debug( LDAP_DEBUG_TRACE,
-                                               LDAP_XSTRING(mdb_search)
-                                               ": candidate %ld not found\n",
-                                               (long) id, 0, 0 );
-                               } else {
-                                       /* get the next ID from the DB */
-                                       rs->sr_err = mdb_get_nextid( mci, &cursor );
-                                       if ( rs->sr_err == MDB_NOTFOUND ) {
-                                               break;
-                                       }
-                                       if ( rs->sr_err ) {
-                                               rs->sr_err = LDAP_OTHER;
-                                               rs->sr_text = "internal error in get_nextid";
-                                               send_ldap_result( op, rs );
-                                               goto done;
-                                       }
-                                       cursor--;
-                               }
-
-                               goto loop_continue;
-                       } else if ( rs->sr_err ) {
-                               rs->sr_err = LDAP_OTHER;
-                               rs->sr_text = "internal error in mdb_id2edata";
-                               send_ldap_result( op, rs );
-                               goto done;
-                       }
-               }
 
                /* Does this candidate actually satisfy the search scope?
                 */
                        /* Fall-thru */
                case LDAP_SCOPE_ONELEVEL:
                        isc.id = id;
-                       if ( mdb_idscopes( op, &isc ) == MDB_SUCCESS ) scopeok = 1;
+                       isc.nscope = 0;
+                       rs->sr_err = mdb_idscopes( op, &isc );
+                       if ( rs->sr_err == MDB_SUCCESS ) {
+                               if ( isc.nscope )
+                                       scopeok = 1;
+                       } else {
+                               if ( rs->sr_err == MDB_NOTFOUND )
+                                       goto notfound;
+                       }
                        break;
                }
 
                        goto loop_continue;
                }
 
-               if ( id != base->e_id ) {
+               if ( id == base->e_id ) {
+                       e = base;
+               } else {
+
+                       /* get the entry */
+                       rs->sr_err = mdb_id2edata( op, mci, id, &edata );
+                       if ( rs->sr_err == MDB_NOTFOUND ) {
+notfound:
+                               if( !MDB_IDL_IS_RANGE(candidates) ) {
+                                       /* only complain for non-range IDLs */
+                                       Debug( LDAP_DEBUG_TRACE,
+                                               LDAP_XSTRING(mdb_search)
+                                               ": candidate %ld not found\n",
+                                               (long) id, 0, 0 );
+                               } else {
+                                       /* get the next ID from the DB */
+                                       rs->sr_err = mdb_get_nextid( mci, &cursor );
+                                       if ( rs->sr_err == MDB_NOTFOUND ) {
+                                               break;
+                                       }
+                                       if ( rs->sr_err ) {
+                                               rs->sr_err = LDAP_OTHER;
+                                               rs->sr_text = "internal error in get_nextid";
+                                               send_ldap_result( op, rs );
+                                               goto done;
+                                       }
+                                       cursor--;
+                               }
+
+                               goto loop_continue;
+                       } else if ( rs->sr_err ) {
+                               rs->sr_err = LDAP_OTHER;
+                               rs->sr_text = "internal error in mdb_id2edata";
+                               send_ldap_result( op, rs );
+                               goto done;
+                       }
+
                        rs->sr_err = mdb_entry_decode( op, &edata, &e );
                        if ( rs->sr_err ) {
                                rs->sr_err = LDAP_OTHER;