MongoDB:从查询结果中排除对象,其中对象字段等于false

时间:2016-03-11 10:11:15

标签: mongodb

如果所述子对象的字段为public class SpringWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getServletConfigClasses() { return new Class[] { SpringWebConfig.class }; } @Override protected Class<?>[] getRootConfigClasses() { return new Class[] { SpringSecurityConfig.class, SpringPersistanceConfig.class }; } @Override protected String[] getServletMappings() { return new String[] { "/", "/rest/*" }; } } @Configuration @EnableWebSecurity public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired @Qualifier("userDetailsService") UserDetailsService userDetailsService; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } @Override protected void configure(HttpSecurity http) throws Exception { http.headers().frameOptions().sameOrigin(); http.authorizeRequests().antMatchers("/assets/**").permitAll().antMatchers("/admin/**").access("hasRole('ROLE_USER')").and().formLogin().loginPage("/login").failureUrl("/login?error").defaultSuccessUrl("/admin").usernameParameter("username").passwordParameter("password").and().logout().logoutSuccessUrl("/login?logout"); } } @Configuration @EnableTransactionManagement @ComponentScan(basePackages = { "com.rgh.*" }, excludeFilters = { @Filter(Controller.class) }) public class SpringPersistanceConfig { @Bean public SessionFactory sessionFactory() { LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource()); builder.scanPackages("com.rgh.*.model").addProperties(getHibernateProperties()); return builder.buildSessionFactory(); } private Properties getHibernateProperties() { Properties properties = new Properties(); properties.put("hibernate.format.sql", "false"); properties.put("hibernate.show.sql", "true"); properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); properties.put("hibernate.hbm2ddl.auto", "update"); return properties; } @Bean(name = "dataSource") public BasicDataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/framework"); dataSource.setUsername("root"); dataSource.setPassword("root"); return dataSource; } @Bean public HibernateTransactionManager txManager() { return new HibernateTransactionManager(sessionFactory()); } } @Controller public class BaseController { @RequestMapping(value = "/admin**", method = RequestMethod.GET) public ModelAndView adminPage() { ModelAndView model = new ModelAndView(); model.setViewName("/panel/admin"); return model; } @RequestMapping(value = "/login", method = RequestMethod.GET) public ModelAndView login() { ModelAndView model = new ModelAndView(); model.setViewName("/panel/login"); return model; } } ,我一直在努力围绕如何从查询中排除子对象。

考虑这个数据集:

local gunXRotated = playerX + r * math.cos(t)
local gunYRotated = playerY + r * math.sin(t)

现在,我不想公开密码,这很容易:

false

但如果{ _id: ObjectId(...), username: ..., password: ..., name: ..., SoMe: { facebook: { id: ..., name: ..., email: ..., public: false } } } db.collection.findOne({ "username": "whatever" }, { "password": 0 }) ,我也只想公开对象SoMe.facebook - 我该怎么做?

2 个答案:

答案 0 :(得分:1)

请使用aggregation试用此版本,并在SoMe.facebook通过SoMe.facebook.public时显示$cond字段。

> db.collection.aggregate([
         {$match: {username:'whatever'}}, 
         {$project: {username: 1, 
                     name:1, 
                     SoMe: {$cond: [{$eq: ['$SoMe.facebook.public', true]}, 
                                   '$SoMe.facebook',
                                    null]}}}
  ])

答案 1 :(得分:0)

db.collection.find({ “SoMe.facebook.public”:真},{ “SoMe.facebook”:1,_id:0})

使用此查询  应该结果像,       SoMe:{           facebook:{             ID: ...,             名称: ...,             电子邮件:...,             公众:是的         }